﻿#region File Description
//-----------------------------------------------------------------------------
// UserInputScreen.cs
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ManagerGame;
using ManagerGame.Screens;
using System.Collections.Generic;

#endregion

namespace ManagerGame
{
    class UserInputScreen : MenuScreen
    {
        #region Initialization

        Manager manager;
        BusinessType companyType;
        Business business; 
        Company company;// the company which is added in this screen
        Company tempCompany; // this is for event handler
        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public UserInputScreen(Business bisnes, Manager manageri)
            : base("Business Menu")
        {
            this.companyType = bisnes.CompanyType;
            manager = manageri;
            business = bisnes;
            this.tempCompany = null;
            MenuEntry businessMenuEntry = null;
            switch (this.companyType)
            {
                case BusinessType.Pub:
                    // Create user input menu entries.
                    businessMenuEntry = new MenuEntry("Buy a pub [20 000 euros]");
                    break;
                case BusinessType.PimpCave:
                    businessMenuEntry = new MenuEntry("Start \"an escort service\"");
                    break;
                case BusinessType.DrugJoint:
                    businessMenuEntry = new MenuEntry("Start selling drugs");
                    break;
                default:
                    businessMenuEntry = new MenuEntry("Not supported business");
                    break;
            }


            MenuEntry backToGameMenuEntry = new MenuEntry("Done");

            // Hook up menu event handlers.
            backToGameMenuEntry.Selected += BackToGameMenuEntrySelected;
            businessMenuEntry.Selected += BusinessMenuEntrySelected;
            


            // Add entries to the menu.
            MenuEntries.Add(businessMenuEntry);
            // this one only if there is something to manage
            if (business.Companies.Count > 0)
            {
                AddAllCompanyMenuEntries();
            }
            MenuEntries.Add(backToGameMenuEntry);
        }
         
        private void AddAllCompanyMenuEntries()
        {
            foreach (Company item in business.Companies)
            {
                MenuEntry manageCompanyEntry = new MenuEntry("[Manage: " + item.toString() + "]");
                tempCompany = item;
                manageCompanyEntry.Selected += manageCompanyEntrySelected;
                MenuEntries.Add(manageCompanyEntry);
            }
        }




        #endregion

        #region Handle Input


        /// <summary>
        /// Event handler for when the Back to Game menu entry is selected.
        /// </summary>
        void BackToGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ExitScreen();
        }


        /// <summary>
        /// Event handler for when the Options menu entry is selected.
        /// </summary>
        void BusinessMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            string businessDone = string.Empty;
            Boolean enoughMoney = true;
            switch (companyType)
            {
                case BusinessType.Pub:
                    
                    // bisness logiikka kussakin case-lohkossa voisi
                    // noudattaa tätä:
                    // onko rahaa?
                    if (manager.Money >= 20000)
                    {
                        //1. tehdään bisnestä eli luodaan firma
                        Company pubi = new Company();
                        // TODO: nämä voitais kokeilla siirtää johonkin managing-ikkunaan
                        pubi.Happiness = Happiness.Content;
                        pubi.Income = 500;
                        pubi.Workers = 1;
                        pubi.Risk = 1;
                        // 2. lisätään listaan


                        business.Companies.Add(pubi);

                        company = pubi; // 'company' is used in businessManagingScreen

                        // 3. vähennetään managerin varoja pubin hinnalla 

                        manager.Money -= 20000;



                        // 4. annetaan infoa
                        // Jos onnistui, niin kerrotaan siitä
                    }
                        //jos ei ollut rahaa
                    else
                    {
                        enoughMoney = false;
                        const string message = "You do not have enough money";

                        MessageBoxScreen didnthavemoney = new MessageBoxScreen(message, false);
                                                
                        ScreenManager.AddScreen(didnthavemoney, null);

                    }


                    /*businessDone = "The pub will cost you 20 000 euros." + Environment.NewLine;
                    businessDone += "After this deal you will have " + 80000 /*theManager.Money + " euros."  + Environment.NewLine;;
                    businessDone += "Good luck with business!";*/
                    // jos pelaajalla on rahaa, niin ok, jos ei jne...



                    // todo: business logic here
                    break;
                case BusinessType.PimpCave:
                    break;
                case BusinessType.DrugJoint:
                    break;
                default:
                    break;
            }
            if (enoughMoney)
            {
                MessageBoxScreen confirmed = new MessageBoxScreen(businessDone);

                confirmed.Accepted += BusinessDoneMessageBoxAccepted;
                //confirmed.Cancelled += BusinessNotAccepted;
                ScreenManager.AddScreen(confirmed, null);
            }

        }

        void BusinessNotAccepted(object sender, PlayerIndexEventArgs e)
        {
           
        }


        /// <summary>
        /// Event handler for when the user accepts the deal
        /// </summary>
        void BusinessDoneMessageBoxAccepted(object sender, PlayerIndexEventArgs e)
        {
            ExitScreen();
        }

       
        /// <summary>
        /// Event handler for when the Frobnicate menu entry is selected.
        /// </summary>
        void LoadGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {

            //
        }

        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            const string message = "Are you sure you want to exit the Manager Game?";

            MessageBoxScreen confirmExitMessageBox = new MessageBoxScreen(message);

            confirmExitMessageBox.Accepted += ConfirmExitMessageBoxAccepted;

            ScreenManager.AddScreen(confirmExitMessageBox, playerIndex);
        }

        protected override void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
        {

            base.OnSelectEntry(entryIndex, playerIndex);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            ScreenManager.Initialize();
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }

      

        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to exit" message box.
        /// </summary>
        void ConfirmExitMessageBoxAccepted(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.Game.Exit();
        }


        #endregion



        void manageCompanyEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ExitScreen();
            foreach (var company in business.Companies)
            {
                BusinessManagingScreen screen = new BusinessManagingScreen(company.toString(), company);
                ScreenManager.AddScreen(screen, e.PlayerIndex);
            }

        }
       
    }
}
